<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;

class ShopPicTransfer extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'shoppic:transfer';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '迁移商铺的图片';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        DB::beginTransaction();

        //取出数据
        $datas = DB::table('s_shanghu')->select('id', 'pic_s', 'pic1', 'pic2', 'pic3', 'phone')->get();
        $pics = [];
        $phones = [];
        if (count($datas) > 0) {
            foreach ($datas as $data) {

                //添加图片数据
                $pics[] = [
                    'shanghu_id' => $data->id,
                    'pic' => $data->pic_s,
                ];
                $pics[] = [
                    'shanghu_id' => $data->id,
                    'pic' => $data->pic1,
                ];
                $pics[] = [
                    'shanghu_id' => $data->id,
                    'pic' => $data->pic2,
                ];
                $pics[] = [
                    'shanghu_id' => $data->id,
                    'pic' => $data->pic3,
                ];

                //联系方式
                $phones[] = [
                    'shanghu_id' => $data->id,
                    'phone_num' => $data->phone
                ];



            }
        }

        if (count($pics) > 0) {
            $pic_insert = DB::table('s_shanghu_pic')->insert($pics);
            if (!$pic_insert) {
                DB::rollBack();

                exit('添加图片失败');
            }
        }
        if (count($phones) > 0) {
            $phone_insert = DB::table('s_shanghu_phone')->insert($phones);
            if (!$phone_insert) {
                DB::rollBack();

                exit('添加联系方式失败');
            }
        }

        DB::commit();

        exit('成功');
    }
}
